

// pages/my/my.js
const app = getApp()
const MRDCommonConst = app.MRDCommonConst()
const UserHttp = app.UserHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    debugger
    this.loadUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  loadUserInfo:async function(){
    const res = await UserHttp.loadDetail();
    this.setData({userInfo:res.result})
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  historyShopTaped:function(){
    wx.navigateTo({
      url: '/pages/my/shop/historyshop',
    })
  },
  couponTaped: function () {
    wx.navigateTo({
      url: '/pages/my/coupon/coupon',
    })
  },
  
  corderTaped: function () {
    wx.navigateTo({
      url: '/pages/my/order/myorder',
    })
  },
  jumpProfile: function () {
    debugger
    if (this.data.userInfo.memberId) {
      wx.navigateTo({
        url: './user/userProfile',
        events: {
          callback: model => {
            this.loadUserInfo()
          }
        },
        success: res => res.eventChannel.emit('dataSource', this.data.userInfo)
      })
    } else {
      this.jumptoLogin()
    }

  },
  jumptoLogin: function () {
    wx.navigateTo({
      url: '/pages/main/common/loginIndex',
    })
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      success: (res) => {
        this.loadUserInfo()
      },
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getSetting:function(){
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          this.getUserInfo();
        }
      }
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    if(e==null)
    {
      this.getSetting();
    }else{
      debugger
      app.globalData.userInfo = e.detail.userInfo
      console.log(app);
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
    
  },
  orderTap:function()
  {

  },
  couponTap: function () {
    wx.navigateTo({
      url: './coupon/coupon',
    })
  }
})
